home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / nihcl-30.lha / nihcl-3.0 / ex / ex14-1.c < prev    next >
C/C++ Source or Header  |  1990-05-15  |  2KB  |  66 lines

  1. // ex14-1.c -- Exception handling in the NIH Class Library
  2.  
  3. // $Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/ex/RCS/ex14-1.c,v 3.0 90/05/15 22:44:55 kgorlen Rel $
  4.  
  5. #include "IdentDict.h"
  6. #include "Dictionary.h"
  7. #include "String.h"
  8. #include "Date.h"
  9. #include "Exception.h"
  10. #include "nihclerrs.h"
  11.  
  12. class Property: public NIHCL {
  13.     static IdentDict prop;      // object property lists
  14. public:
  15.     static Object* add(Object& ob, String& name, Object& value);
  16.     static Object* get(const Object& ob, const String& name);
  17. };
  18.  
  19. IdentDict Property::prop;
  20.  
  21. Object* Property::add(
  22.     Object& ob,         // object to receive property
  23.     String& name,       // name of property
  24.     Object& value)      // property value
  25. {
  26.     Object* oldvalue = &value;
  27.     Dictionary* d;
  28.     if (!prop.includesKey(ob)) {
  29.         d = new Dictionary;
  30.         d->addAssoc(*new String(name),value);
  31.         prop.addAssoc(ob,*d);
  32.     }
  33.     else {
  34.         d = (Dictionary*)prop.atKey(ob);
  35.         if (d->includesKey(name))
  36.             oldvalue = d->atKey(name,value);
  37.         else d->addAssoc(*new String(name),value);
  38.     }
  39.     return oldvalue;
  40. }
  41.  
  42. Object* Property::get(
  43.     const Object& ob,   // object with property
  44.     const String& name) // name of property
  45. {
  46.     RaiseException x(NIHCL__KEYNOTFOUND);
  47.     BEGINX
  48.         return ((Dictionary*)prop.atKey(ob))->atKey(name);
  49.     EXCEPTION
  50.         case NIHCL__KEYNOTFOUND: return Object::nil;
  51.         default: RAISE(EXCEPTION_CODE);
  52.     ENDX
  53. }
  54.  
  55. main()
  56. {
  57.     String gregory = "person";
  58.     Property::add(gregory,*new String("birthdate"),
  59.                   *new Date(10,"Mar",86));
  60.     Property::add(gregory,*new String("eye-color"),
  61.                   *new String("brown"));
  62.     cout << *Property::get(gregory,"birthdate") << endl;
  63.     cout << *Property::get(gregory,"eye-color") << endl;
  64.     cout << *Property::get(gregory,"weight") << endl;
  65. }
  66.